﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestWin
{
    public partial class Form1 : Form, See51.IGcSee51AvCall
    {
        private long m_lUiHandel=0;
       // c84439000001","name":"ipcamera","dataserver":"ssp://39.108.213.89

        //private string m_strDevId  = "c84408000318";
        //private string m_strTurnIp = "123.57.15.129";

        private string m_strDevId = "c84439000001";
        private string m_strTurnIp = "39.108.213.89";
        private string m_strGps = "";
        int Okkk = 0;
        long m_ListenerHandle = 0;
        public void SetIpInfo(string strDevId, string strTurnIp)
        {
            m_strDevId = strDevId;
            m_strTurnIp = strTurnIp;
        }
        public int GetListenerType()
        {
            return 2|16;
        }
        //0000 0001： 1 文件      0000 0010：2   断开监听   0000 0100：  4 音频监听 
        //0000 1000:  8 视频监听  0001 0000：16  报警监听
        public void OnSeeAv(String deviceId, See51.SFrameHead head, Byte[] data)
        {


        }
        public void OnAlarm(String deviceId, See51.SAlarmInfo alarm)
        {
            int ii = 0;
            ii++;
            Okkk = 1;
            m_strGps = alarm.gpsType+";" + alarm.direction+":"+
            alarm.gpsType + ":" +
            alarm.latitude + ":" +
            alarm.longitude + ":" +
            alarm.speed + ":" +
            alarm.time;

    }
        //设备断开通知，此函数内不能实现关于设备任何的操作，仅仅作为通知
        public void OnClose(string deviceId)
        {

        }
        public void OnDownFileOver(string deviceId, string fileName)
        {

        }
        public long GetObjHandle()
        {
            return m_ListenerHandle;
        }
        public void SeetObjHandle(long handle)
        {
            m_ListenerHandle = handle;
        }
        // ("c84415000438", "39.108.213.89");
        public Form1()
        {
           
           InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IntPtr xx = this.Handle;
            m_lUiHandel = See51.CSharpSee51Sdk.CreateVideoUI(xx.ToInt64());

            See51.CSharpSee51Sdk.SetWinUIPos(m_lUiHandel, 0, 0, 500, 400);
            See51.CSharpSee51Sdk.SetWinUIVisble(m_lUiHandel, 1);

            //9个窗口
            //See51.CSharpSee51Sdk.SetWinUIRowCol(m_lUiHandel, 3, 3);
            //一个窗口
            See51.CSharpSee51Sdk.SetWinUIRowCol(m_lUiHandel, 1, 1);

            string acc = "test2019bm";
            string pass = "test2019bm";
            //登录服务器，拿到口令等信息
            String json=  See51.CSharpSee51Sdk.Login51See(acc, pass);

            if (json != null) {
                //从json字段里获取口令信息
                String access_token = See51.CSharpSee51Sdk.GetSimpleJsonFiled(json, "access_token");
                if (access_token != null)
                { 
                    //获取设备列表
                    String DevList = See51.CSharpSee51Sdk.Get51SeeGroupDevs(0, acc, access_token);
                    String devInfos = See51.CSharpSee51Sdk.GetSimpleJsonFiled(DevList, "device_info");


                    //设备动作
                    //action 	镜像15   16
                    // See51.CSharpSee51Sdk.SetDevAction
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int DevH = sdk.JniGetDevHandle("c21621000011", "202.100.177.36");
           
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId ,m_strTurnIp); 
            See51.CSharpSee51Sdk.ShowDevVideoByWinUI(m_lUiHandel, 1, 1, m_strDevId, m_strTurnIp, "Test");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.WinUIItemClearData(m_lUiHandel, 1, 1);
            See51.CSharpSee51Sdk.See51SDKStopDevVideo(hh);
            int ii=See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.SetDevAction(hh, 15);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.SetDevAction(hh, 16);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_Up_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.SetDevAction(hh, 3);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_right_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.SetDevAction(hh, 2);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_left_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.SetDevAction(hh, 1);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            See51.CSharpSee51Sdk.SetDevAction(hh, 4);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void ckListBox_Video_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            CheckedListBox box = (CheckedListBox)sender;
            int index = box.SelectedIndex;
            if (index == 0) {
                //max
                See51.CSharpSee51Sdk.See51SDKSetDevVideoQuality(hh,1);
            }
            else if (index == 1)
            {
                //mid
                See51.CSharpSee51Sdk.See51SDKSetDevVideoQuality(hh, 2);
            }
            else if (index == 2)
            {
                //min
                See51.CSharpSee51Sdk.See51SDKSetDevVideoQuality(hh, 3);
            }
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_SetRtmp_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            string rtmp = textBox_Rtmp.Text;
            See51.CSharpSee51Sdk.See51SDKSendDevRtmp(hh, rtmp, 3003, 0);
            int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            string rtmp = textBox_Rtmp.Text;
            int ii = See51.CSharpSee51Sdk.Add51SeeListener(hh, this);
            ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            string rtmp = textBox_Rtmp.Text;
            int ii = See51.CSharpSee51Sdk.Release51SeeListener(hh, this);
            ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void btn_art_Click(object sender, EventArgs e)
        {
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            string rtmp = textBox_Rtmp.Text;
            int ii = See51.CSharpSee51Sdk.See51SDKRequestAlarm(hh);
            ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
            ii = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (Okkk == 1)
            {
                Okkk = 0;
                // richTextBox1.Clear();
                richTextBox1.AppendText(m_strGps);
            }
        }
    }
}
